from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging
from funasr.models.transformer.embedding import SinusoidalPositionEncoder

logger = logging.get_logger(__name__)


class DoAsrConfig(PretrainedConfig):
    model_type = "do_asr"
    keys_to_ignore_at_inference = ["past_key_values"]

    def __init__(
        self,
        vocab_size=151936,
        hidden_size=4096,
        intermediate_size=22016,
        num_hidden_layers=32,
        num_attention_heads=32,
        num_key_value_heads=32,
        hidden_act="silu",
        max_position_embeddings=32768,
        initializer_range=0.02,
        rms_norm_eps=1e-6,
        use_cache=True,
        rope_theta=10000.0,
        attention_dropout=0.1,
        audio_encoder_output_size=512,
        audio_encoder_attention_heads=4,
        audio_encoder_linear_units=2048,
        audio_encoder_num_blocks=50,
        audio_encoder_dropout_rate=0.1,
        audio_encoder_positional_dropout_rate=0.1,
        audio_encoder_attention_dropout_rate=0.1,
        audio_encoder_input_layer="pe",
        audio_encoder_pos_enc_class=SinusoidalPositionEncoder,
        audio_encoder_normalize_before=True,
        audio_encoder_kernel_size=11,
        audio_encoder_sanm_shfit=0,
        audio_encoder_selfattention_layer_type="sanm",
        cif_idim=512,
        cif_threshold=1.0,
        cif_l_order=1,
        cif_r_order=1,
        cif_tail_threshold=0.45,
        cif_smooth_factor2=0.25,
        cif_noise_threshold2=0.01,
        cif_upsample_times=3,
        cif_use_cif1_cnn=False,
        cif_upsample_type="cnn_blstm",
        wav_frontend_fs=16000,
        wav_frontend_window="hamming",
        wav_frontend_n_mels=80,
        wav_frontend_frame_length=25,
        wav_frontend_frame_shift=10,
        wav_frontend_lfr_m=7,
        wav_frontend_lfr_n=6,
        specaug_apply_time_warp=False,
        specaug_time_warp_window=5,
        specaug_time_warp_mode="bicubic",
        specaug_apply_freq_mask=True,
        specaug_freq_mask_width_range=[0, 30],
        specaug_lfr_rate=6,
        specaug_num_freq_mask=1,
        specaug_apply_time_mask=True,
        specaug_time_mask_width_range=[0, 12],
        specaug_num_time_mask=1,
        **kwargs,
    ):
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads

        # for backward compatibility
        if num_key_value_heads is None:
            num_key_value_heads = num_attention_heads

        self.num_key_value_heads = num_key_value_heads
        self.hidden_act = hidden_act
        self.initializer_range = initializer_range
        self.rms_norm_eps = rms_norm_eps
        self.use_cache = use_cache
        self.rope_theta = rope_theta
        self.attention_dropout = attention_dropout

        super().__init__(
            # tie_word_embeddings=True,
            **kwargs,
        )
